package com.ttbj.product.api.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.ttbj.constant.order.PayStateEnum;
import com.ttbj.constant.order.StateEnum;
import com.ttbj.constant.salesman.WorkStateEnum;
import com.ttbj.freight.component.OrderStateComponent;
import com.ttbj.product.api.OrderFacade;
import com.ttbj.product.api.WorkOrderFacade;
import com.ttbj.product.bean.dto.OrderDto;
import com.ttbj.product.bean.dto.PageGlobal;
import com.ttbj.product.bean.dto.WorkListDto;
import com.ttbj.util.Assert;
import com.ttbj.work.dao.WorkCustomMapper;
import com.ttbj.work.dao.WorkMapper;
import com.ttbj.work.po.Work;

@Service("workOrderFacade")
public class WorkOrderFacadeImpl implements WorkOrderFacade {

	@Autowired
	private WorkCustomMapper workCustomMapper = null;
	
	@Autowired
	private WorkMapper workMapper = null;
	
	@Autowired
	private OrderFacade orderFacade = null;
	
	@Autowired
	private  OrderStateComponent orderStateComponent = null;
	
	@Override
	public List<WorkListDto> getWorkList(Long salesmanId) throws Exception {
		return workCustomMapper.workList(salesmanId);
	}

	@Override
	public OrderDto getWorkOrderDetail(Long orderId) throws Exception {
		OrderDto orderDetail = orderFacade.complexDetailOrder(orderId);
		return orderDetail;
	}
	

	@Override
	public void confirm(Long workId, Long orderId, Float allFee, Float fee, Float carryFee, Float packFee, Integer orderToCarry, Integer orderToPack, String truckType, Integer orderDriverRequire, Integer floorNum, Integer baleNum) throws Exception {
		workMapper.updateByPrimaryKeySelective(new Work (workId,WorkStateEnum.RLREADY.getConfirm()));//0:接单状态 (工单未处理)1：上门确认（确认工单）
		OrderDto orderDto = new OrderDto(orderId,StateEnum.SALESMAN_CONFIRM_ORDER.getCode(),PayStateEnum.NONE.getState(),allFee,fee,carryFee,packFee,orderToCarry,orderToPack).truckType(truckType).requireTruck(orderDriverRequire);
		orderDto.setOrderFloorNum(floorNum);
		orderDto.setOrderBoxNum(baleNum);
		orderStateComponent.addOrderStateCache(orderId, orderDriverRequire);
		orderFacade.updateOrder(orderDto);
	}

	@Override
	public List<WorkListDto> getWorkList(Long salesmanId, Integer isCompl, PageGlobal page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		List<WorkListDto>  list = workCustomMapper.workListProcess(salesmanId,isCompl);
		return list;
	}

	@Override
	public Long getWorkOrder(Long workId) throws Exception {
		Work work = workMapper.selectByPrimaryKey(workId);
		Assert.assertNonNullField(work, 405, "workOrder");
		return work.getWorkOrder();
	}
	
	
	
	
	

}
